﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Linq.SqlClient;
using FilstartupsREST.Models;

namespace FilstartupsREST.FILSTARTUPSService.rest.Controllers
{
    public class CategoryController : Controller
    {
        FilStartUpsEntities cms_db = new FilStartUpsEntities();
        //
        // GET: /Category/
        [HttpGet]
        public JsonResult GetCategoryInfo()
        {
            var category = cms_db.Categories.Where(c => c.routingidFK == 2).ToList();

            if (category.Count() > 0)
            {
                return Json(new
                    {
                        data = category,
                        success = true
                    }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new
                    {
                        data = string.Empty
                    }, JsonRequestBehavior.AllowGet);
            }
        }
        //GET SEARCH POST ->CATEGORY
        //id=>catid
        //searchquery=>string to search
        [HttpGet]
        public JsonResult GetSearchCategory(string searchquery, string id)
        {
            int catid = int.Parse(id);

            if (catid == -1)
            {
                var data = (from c in cms_db.Categories
                            join p in cms_db.Posts
                                on c.catidPK equals p.catidFK
                            where p.posttitle.Contains(searchquery.Trim())
                            && p.routingidFK.Equals(2)
                            select new
                            {
                                postidPK = p.postidPK,
                                posttitle = p.posttitle,
                                postdescription = p.postdescription,
                                datecreated = p.datecreated
                            }).ToList();
                if (data.Count() > 0)
                {
                    return Json(new { data }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
                }

            }
            else
            {
                var data = (from c in cms_db.Categories
                            join p in cms_db.Posts
                                on c.catidPK equals p.catidFK
                            where p.posttitle.Contains(searchquery.Trim())
                             && c.catidPK.Equals(catid)
                             && p.routingidFK.Equals(2)
                            select new
                            {
                                postidPK = p.postidPK,
                                posttitle = p.posttitle,
                                postdescription = p.postdescription,
                                datecreated = p.datecreated
                            }).ToList();

                if (data.Count() > 0)
                {
                    return Json(new { data }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
                }

            }


        }

        //GET LATEST POST
        [HttpGet]
        public JsonResult GetCategoryPost(string id)
        {
            int catid_ = int.Parse(id);

            var catpost = (from p in cms_db.Posts
                           join c in cms_db.Categories
                           on p.catidFK equals c.catidPK
                           where p.catidFK.Equals(catid_)
                           && p.routingidFK.Equals(2)
                           orderby p.postidPK descending
                           select new CategoryPost
                           {
                               catidFK = c.catidPK,
                               postdescription = p.postdescription,
                               posttitle = p.posttitle,
                               postidPK = p.postidPK,
                               pageidFK = c.pageidFK
                           }).FirstOrDefault();

            if (catpost != null)
            {
                return Json(new { catpost, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { catpost = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        //GET PAGE SUB CATEGORY
        [HttpGet]
        public JsonResult GetPageSub(string id)
        {
            int pageidPK = int.Parse(id);

            var pagesub = (from c in cms_db.Categories
                           join p in cms_db.Pages
                              on c.pageidFK equals p.pageidPK
                              let cCount =(from po in cms_db.Posts
                                         where po.catidFK.Equals(c.catidPK)
                                            select po).Count()
                           where c.pageidFK.Equals(pageidPK)
                               && c.routingidFK.Equals(2) && 
                               cCount != 0
                           select c).ToList();

            //cms_db.Categories.Where(c => c.pageidFK.Equals(pageidPK)
            //                                    && c.routingidFK.Equals(2)).ToList();

            if (pagesub.Count() > 0)
            {
                return Json(new
                {
                    pagesub
                }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(
                    new
                    {
                        pagesub = string.Empty
                    }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
